//
//  VCLogin.m
//  MOSAD_HW2_1
//
//  Created by Jason on 2016/10/29.
//  Copyright © 2016 Jason. All rights reserved.
//

#import "VCLogin.h"

@interface VCLogin ()

@end

@implementation VCLogin

- (instancetype)init
{
    self = [super init];
    if (self) {
        UIImage * image = [UIImage imageNamed:[NSString stringWithFormat:@"myself.png"]];
        //修改图片大小
        UIGraphicsBeginImageContext(CGSizeMake(32, 32));
        [image drawInRect:CGRectMake(0, 0, 32, 32)];
        UIImage *reSizeImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        //创建一个分栏按钮对象
        UITabBarItem * tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我的" image:reSizeImage tag:3];
        
        self.tabBarItem = tabBarItem;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.navigationItem setTitle:@"我的"];
    self.navigationController.navigationBar.translucent = NO;
    
    //添加渐变背景
    //创建CGContextRef
    UIGraphicsBeginImageContext(self.view.bounds.size);
    CGContextRef gc = UIGraphicsGetCurrentContext();
    //创建CGMutablePathRef
    CGMutablePathRef path = CGPathCreateMutable();
    //绘制Path
    CGRect rect = CGRectMake(-600, 0, 1000, 1000);
    CGPathMoveToPoint(path, NULL, CGRectGetMinX(rect), CGRectGetMinY(rect));
    CGPathAddLineToPoint(path, NULL, CGRectGetMidX(rect), CGRectGetMaxY(rect));
    CGPathAddLineToPoint(path, NULL, CGRectGetWidth(rect), CGRectGetMaxY(rect));
    CGPathAddLineToPoint(path, NULL, CGRectGetWidth(rect), CGRectGetMinY(rect));
    CGPathCloseSubpath(path);
    //绘制渐变
    [self drawRadialGradient:gc path:path startColor:[UIColor whiteColor].CGColor endColor:[UIColor grayColor].CGColor];
    //注意释放CGMutablePathRef
    CGPathRelease(path);
    //从Context中获取图像，并显示在界面上
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
    [self.view addSubview:imgView];
    
    
    
    //添加按钮
    UIView * loginButton = [[UIView alloc] init];
    loginButton.frame = CGRectMake(0, 0, 300, 300);
    //居中显示
    loginButton.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2, [UIScreen mainScreen].bounds.size.height/2-100);
    //圆的半径是loginButton宽的一半
    loginButton.layer.cornerRadius = loginButton.frame.size.width / 2;
    //是否进行剪切
    loginButton.clipsToBounds = YES;
    //设置登录按钮颜色
    loginButton.backgroundColor = [UIColor whiteColor];
    
    //设置边框大小
    loginButton.layer.borderWidth = 1.5;
    //边框颜色
    loginButton.layer.borderColor = [[UIColor blackColor] CGColor];
    
    UILabel * login = [[UILabel alloc] init];
    login.text = @"登录";
    login.frame = CGRectMake(0, 0, 100, 100);
    login.center = CGPointMake(loginButton.bounds.size.width/2, loginButton.bounds.size.height/2);
    //设置字体大小颜色
    login.font = [UIFont systemFontOfSize:40];
    login.textColor = [UIColor blackColor];
    //设置对齐模式，默认左对齐
    login.textAlignment = NSTextAlignmentCenter;
    
    [loginButton addSubview:login];
    
    [self.view addSubview:loginButton];
    
    //点击登录按钮登录
    //打开loginButton的用户交互
    loginButton.userInteractionEnabled = YES;
    //添加手势
    UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pressLogin:)];
    //将手势添加到loginButton
    [loginButton addGestureRecognizer:tapGesture];
    //选择触发事件的方式（默认单击触发）
    [tapGesture setNumberOfTapsRequired:1];
    
    
    
    
}

//实现圆半径方向颜色渐变
- (void)drawRadialGradient:(CGContextRef)context
path:(CGPathRef)path
startColor:(CGColorRef)startColor
endColor:(CGColorRef)endColor
{
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGFloat locations[] = { 0.0, 1.0 };
    
    NSArray *colors = @[(__bridge id) startColor, (__bridge id) endColor];
    
    CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef) colors, locations);
    
    
    CGRect pathRect = CGPathGetBoundingBox(path);
    CGPoint center = CGPointMake(CGRectGetMidX(pathRect), CGRectGetMidY(pathRect));
    CGFloat radius = MAX(pathRect.size.width / 2.0, pathRect.size.height / 2.0) * sqrt(2);
    
    CGContextSaveGState(context);
    CGContextAddPath(context, path);
    CGContextEOClip(context);
    
    CGContextDrawRadialGradient(context, gradient, center, 0, center, radius, 0);
    
    CGContextRestoreGState(context);
    
    CGGradientRelease(gradient);
    CGColorSpaceRelease(colorSpace);
}


-(void) pressLogin : (UITapGestureRecognizer *) gesture {
    VCLoginMessage * vcLoginMessage = [[VCLoginMessage alloc] init];
    [self.navigationController pushViewController:vcLoginMessage animated:YES];
}



/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
